package com.industrial.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.domain.SysRole;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import com.industrial.system.contents.RoleContents;
import com.industrial.system.domain.SysDataReporting;
import com.industrial.system.mapper.SysDataReportingMapper;
import com.industrial.system.service.ISysDataReportingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


/**
 * 数据上报Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-18
 */
@Service
public class SysDataReportingServiceImpl  implements ISysDataReportingService
{
    @Autowired
    private SysDataReportingMapper sysDataReportingMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询数据上报
     *
     * @param baseId 数据上报主键
     * @return 数据上报
     */
    @Override
    public SysDataReporting selectSysDataReportingByBaseId(String baseId)
    {
        SysDataReporting sysDataReporting = sysDataReportingMapper.selectSysDataReportingByBaseId(baseId);
        List<String> address = new ArrayList<>();

        if(StringUtils.isNotBlank(sysDataReporting.getProvince())
                && StringUtils.isNotBlank(sysDataReporting.getCity())
                && StringUtils.isNotBlank(sysDataReporting.getDistrict())){
            address.add(sysDataReporting.getProvince());
            address.add(sysDataReporting.getCity());
            address.add(sysDataReporting.getDistrict());
        }else if(StringUtils.isNotBlank(sysDataReporting.getProvince()) && StringUtils.isNotBlank(sysDataReporting.getCity())){
            address.add(sysDataReporting.getProvince());
            address.add(sysDataReporting.getCity());
        }else if(StringUtils.isNotBlank(sysDataReporting.getProvince())){
            address.add(sysDataReporting.getProvince());
        }
        sysDataReporting.setAddress(address);
        return sysDataReporting;
    }

    /**
     * 查询数据上报列表
     *
     * @param sysDataReporting 数据上报
     * @return 数据上报
     */
    @Override
    public List<SysDataReporting> selectSysDataReportingList(SysDataReporting sysDataReporting)
    {
        List<String> addressArgs = sysDataReporting.getAddress();
        if(addressArgs != null && !addressArgs.isEmpty()){
            if(addressArgs.size() == 1){
                sysDataReporting.setProvince(addressArgs.get(0));
            }else if(addressArgs.size() == 2){
                sysDataReporting.setProvince(addressArgs.get(0));
                sysDataReporting.setCity(addressArgs.get(1));
            }else if(addressArgs.size() == 3){
                sysDataReporting.setProvince(addressArgs.get(0));
                sysDataReporting.setCity(addressArgs.get(1));
                sysDataReporting.setDistrict(addressArgs.get(2));
            }
        }

        List<SysDataReporting> sysDataReportings = sysDataReportingMapper.selectSysDataReportingList(sysDataReporting);
        for(SysDataReporting item : sysDataReportings){
            String province = item.getProvince();
            String city = item.getCity();
            String district = item.getDistrict();

            StringBuilder address = new StringBuilder();

            if(StringUtils.isNotBlank(province)){
                String provinceName = redisTemplate.opsForValue().get("area:" + province);
                address.append(provinceName);
            }

            if(StringUtils.isNotBlank(city)){
                String cityName = redisTemplate.opsForValue().get("area:" + city);
                address.append("-").append(cityName);
            }

            if(StringUtils.isNotBlank(district)){
                String districtName = redisTemplate.opsForValue().get("area:" + district);
                address.append("-").append(districtName);
            }
            item.setAddressArea(address.toString());
        }
        return sysDataReportings;
    }

    /**
     * 新增数据上报
     *
     * @param sysDataReporting 数据上报
     * @return 结果
     */
    @Override
    public int insertSysDataReporting(SysDataReporting sysDataReporting)
    {
        sysDataReporting.setBaseId(IdUtils.simpleUUID());
        sysDataReporting.setBaseCreateTime(DateUtils.getNowDate());
        sysDataReporting.setBaseCreateBy(SecurityUtils.getUserId());
        List<String> address = sysDataReporting.getAddress();
        if(address != null && !address.isEmpty()){
            if(address.size() == 1){
                sysDataReporting.setProvince(address.get(0));
            }else if(address.size() == 2){
                sysDataReporting.setProvince(address.get(0));
                sysDataReporting.setCity(address.get(1));
            }else if(address.size() == 3){
                sysDataReporting.setProvince(address.get(0));
                sysDataReporting.setCity(address.get(1));
                sysDataReporting.setDistrict(address.get(2));
            }
        }
        sysDataReporting.setCheckStatus("0");
        return sysDataReportingMapper.insertSysDataReporting(sysDataReporting);
    }

    /**
     * 修改数据上报
     *
     * @param sysDataReporting 数据上报
     * @return 结果
     */
    @Override
    public int updateSysDataReporting(SysDataReporting sysDataReporting)
    {
        List<String> address = sysDataReporting.getAddress();
        if(address != null && !address.isEmpty()){
            if(address.size() == 1){
                sysDataReporting.setProvince(address.get(0));
            }else if(address.size() == 2){
                sysDataReporting.setProvince(address.get(0));
                sysDataReporting.setCity(address.get(1));
            }else if(address.size() == 3){
                sysDataReporting.setProvince(address.get(0));
                sysDataReporting.setCity(address.get(1));
                sysDataReporting.setDistrict(address.get(2));
            }
        }
        sysDataReporting.setCheckStatus("0");
        return sysDataReportingMapper.updateSysDataReporting(sysDataReporting);
    }

    /**
     * 批量删除数据上报
     *
     * @param baseIds 需要删除的数据上报主键
     * @return 结果
     */
    @Override
    public int deleteSysDataReportingByBaseIds(String[] baseIds)
    {
        return sysDataReportingMapper.deleteSysDataReportingByBaseIds(baseIds);
    }

    /**
     * 删除数据上报信息
     *
     * @param baseId 数据上报主键
     * @return 结果
     */
    @Override
    public int deleteSysDataReportingByBaseId(String baseId)
    {
        return sysDataReportingMapper.deleteSysDataReportingByBaseId(baseId);
    }

    /**
     * 市级审核数据
     * @param sysDataReporting
     * @return
     */
    @Override
    public AjaxResult cityCheckData(SysDataReporting sysDataReporting) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if(Objects.isNull(sysUser)){
            return AjaxResult.warn("没有找到用户信息");
        }
        List<SysRole> roles = sysUser.getRoles();
        if(roles == null || roles.isEmpty()){
            return AjaxResult.warn("没有找到用户角色");
        }
        List<String> userRoleArgs = roles.stream().map(SysRole::getRoleKey).collect(Collectors.toList());
        if(!ArrayUtil.containsAny(userRoleArgs.toArray(), RoleContents.PROVINCE,RoleContents.CITY,RoleContents.DISTRICT)){
            return AjaxResult.warn("您没有权限审核数据");
        }
        SysDataReporting params = new SysDataReporting();
        params.setBaseId(sysDataReporting.getBaseId());
        params.setCheckStatus(sysDataReporting.getCheckStatus());
        if(StringUtils.isNotBlank(sysDataReporting.getRejection())){
            params.setRejection(sysDataReporting.getRejection());
        }
        sysDataReportingMapper.updateCheckStatus(params);
        return AjaxResult.success("审核完成");
    }
}
